package com.rem.flink.flink2Source;

import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.ArrayList;


/**
 * 读取数据的类型
 *
 * @author Rem
 * @date 2022/10/9
 */
public class SourceTest {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        // 1. 从文件中读取数据
        //  readTextFile(env);

        //   2. 从集合中读取数据
        // fromCollection(env);
//
//        // 3. 从元素读取数据
        // fromElements(env);
//
//        // 4. 从Socket文本流读取
        socketTextStream(env);

        env.execute();
    }

    private static void socketTextStream(StreamExecutionEnvironment env) {
        DataStreamSource<String> stream4 = env.socketTextStream("localhost", 7777);
        stream4.print("4");
    }

    private static void fromElements(StreamExecutionEnvironment env) {
        DataStreamSource<Event> stream3 = env.fromElements(
                new Event("Mary", "./home", 1000L),
                new Event("Bob", "./cart", 2000L)
        );
        stream3.print();
    }

    private static void fromCollection(StreamExecutionEnvironment env) {
        ArrayList<Integer> nums = new ArrayList<>();
        nums.add(2);
        nums.add(5);
        DataStreamSource<Integer> numStream = env.fromCollection(nums);
        numStream.print();

        ArrayList<Event> events = new ArrayList<>();
        events.add(new Event("Mary", "./home", 1000L));
        events.add(new Event("Bob", "./cart", 2000L));
        DataStreamSource<Event> stream2 = env.fromCollection(events);
        stream2.print();
    }

    private static void readTextFile(StreamExecutionEnvironment env) {

        DataStreamSource<String> stream1 = env.readTextFile("input/clicks.csv");
        stream1.print();
    }
}
